home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Recursion
- Date: 3 Apr 1996 07:15:35 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4ju4mnINN1qb@keats.ugrad.cs.ubc.ca>
- References: <31624BC2.70D2@sooner.net>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <31624BC2.70D2@sooner.net>, Eddie Bush <edwbush@sooner.net> wrote:
- >I am trying to construct a C function that will recursively convert
- >a string such as "1234" into it's integer equivelant (1234).
- >
- >Here is what I know:
- >1)if you subtract the character "0" from any of the other digits "1".."9"
- > you will get the integer value of that characer.
- > Example: "1" - "0" is equal to 1
- > "2" - "0" is equal to 2
- > .
- > .
- > .
- > "9" - "0" is equal to 9
- >2)the function should be called with a character pointer:
- > Such as: convert("1234");
- > making the prototype look something like:
- > int convert(char *p);
- >
- >Does anyone have an idea? This is sorta stumping me. I am aware of
- >atoi, but I am wanting to write a recursive function that does that --
- >for the fun of it. It's sort of a little puzzle to help me learn
- >recursion. Any ideas?
-
- Sounds more like a puzzle to earn a grade in a introductory programming course.
- Nobody would waste time by rewriting atoi() using recursion, unless it's
- homework. Plus if you really wanted a challenging puzzle, would you not solve
- it yourself?
-
- In any case, you can use a "multiply and surrender approach" (which is a name
- given to any mis-application of the divide and conquer approach).
-
- The integer value of a string can be recursively defined as the value of the
- least significant digit, plus the value of the rest of the string times 10.
-
- For example, atoi("12345") == atoi("1234") * 10 + atoi("5");
-
- This is one way to define it recursively.
-
- I recommend that you reverse the digits contained in the string first. This may
- involve making a static copy of the string, or making an agreement with the
- caller that the string will be modified. Or you can make a local copy of the
- string which you reverse. Either way, you will probably want a ``wrapper''
- function which reverses the digits and then calls the actual function, let's
- call it val().
-
- With the digits reversed, the value of "1234" is just val("4321") which is
- just 4 + 10 * val("321"). It is much easier to extract the digits from the
- beginning of a string just by advancing a pointer rather than searching to the
- end of a string in each recursive call.
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define VAL(c) ((c) - '0')
-
- long val(const char *str)
-
- /* no overflow or illegal char checking */
- /* takes a _reversed_ digit string */
-
- {
- if(!*str) /* at the end of string, return zero */
- return 0;
- else
- return /* you ``puzzle'' this part out! */;
- }
-
-
- long ratoi(const char *str)
-
- {
- char *tmp = malloc(strlen(str) + 1);
- int value;
-
- /* should be checking for malloc() failure */
-
- strcpy(tmp, str);
-
- /* now reverse it - look up in K&R2 how to do this or figure out */
-
- value = val(tmp);
- free(tmp);
-
- return val;
- }
- --
-
-